package linkedlist;

/**
 * Created by Tzachi on 14/10/2014.
 */
public class ReverseList {

    public static Node reverseList(Node head) {
        if (head == null || head.getNext() == null) {
            return head;
        }
        Node prev = null;
        Node curr = head;
        Node next;
        while (curr != null) {
            next = curr.getNext();
            curr.setNext(prev);
            prev = curr;
            curr = next;
        }
        return prev;
    }

    public static void main(String[] args) {
        Node head = new Node(4);
        head.chainNext(new Node(8)).chainNext(new Node(8)).chainNext(new Node(12));
        System.out.println("before: ");
        head.printChain();
        head = reverseList(head);
        System.out.println("after: ");
        head.printChain();

    }
}
